% Author: Feng Sheng
% Study: Buysell
% Design: Within-subject; Each subject makes 10*10=100 decisions in a block
% when playing either the role of a buyer or the role of a seller

%% Aim
% Prepare trial-level data for aDDM analysis using HDDM

%% Setup
clc;
clear;
root_path = pwd; 
addpath(genpath(root_path));

%% Load data
beh_data=readtable([root_path,'\data\behavior\behavior.csv']);
sub_data=readtable([root_path,'\data\subject\subject.csv']);
gaze_trial_data=readtable([root_path,'\data\gaze\gaze_trial.csv']);
eye_trial_data=readtable([root_path,'\data\eye\eye_trial.csv']);
nsub=height(sub_data);

%% Concatenated trial-level data
trial_data = [beh_data, eye_trial_data(:,10:end), gaze_trial_data(:,10:end)];

%% aDDM parameters
trial_data.LotteryGazeDurLotteryRatio=0.5.*trial_data.Lottery.*trial_data.GazeDurLotteryRatio;
trial_data.LotteryGazeDurPriceRatio=0.5.*trial_data.Lottery.*(1-trial_data.GazeDurLotteryRatio);

trial_data.PriceGazeDurLotteryRatio=trial_data.Price.*trial_data.GazeDurLotteryRatio;
trial_data.PriceGazeDurPriceRatio=trial_data.Price.*(1-trial_data.GazeDurLotteryRatio);

%% Save data for HDDM involving gaze or pupil information

% End up with lottery=1, End up with money=0;
hddm_data=trial_data(trial_data.ValidGaze==1 & trial_data.GazeNum>0,:);
hddm_data.Properties.VariableNames{'Subject'} = 'subj_idx';
hddm_data.Properties.VariableNames{'Ending'} = 'response';
hddm_data.Properties.VariableNames{'RT'} = 'rt';
hddm_data.Lottery=0.5.*hddm_data.Lottery;
writetable(hddm_data,[root_path,'\model\hddm\trial_hddm_eye.csv']);
